<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-form size="small" label-width="70px" v-formkey="{query:toQuery}">
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="编码">
                <el-input v-model="query.code" placeholder="输入编码进行搜索"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="名称">
                <el-input v-model="query.name" placeholder="输入名称进行搜索"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="客户名称">
                <zSelect :value.sync="query.customerId" url="api/dropDown/basCustomerList" style="width: 100%;"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
<!--              <el-form-item label="状态">-->
<!--                <el-select v-model="query.status" class="edit-input" placeholder="请选择状态" style="width: 100%;">-->
<!--                  <el-option-->
<!--                      v-for="(item,index) in dict.common_status"-->
<!--                      :key="index"-->
<!--                      v-if="item.seq>0"-->
<!--                      :label="item.label"-->
<!--                      :value="item.value"-->
<!--                  />-->
<!--                </el-select>-->
<!--              </el-form-item>-->
            </el-col>
          </el-row>
        </el-form>
      <rrOperation :crud="crud"/>
      </div>
      <crudOperation :permission="permission">
      </crudOperation>
    </div>
    <div class="body-container">
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="80vw">
        <el-form ref="form" :model="form" :rules="rules" size="small" :inline="true" label-width="80px">
          <el-form-item label="编码" prop="code">
            <el-input v-model="form.code" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;"/>
          </el-form-item>

          <el-tabs v-model="activeName" type="card">
            <el-tab-pane label="基本信息" name="info">
              <el-form-item label="线路复杂度">
                <el-select v-model="form.type" class="edit-input" placeholder="请选择线路类型" style="width: 370px;">
                  <el-option
                      v-for="(item,index) in dict.t_bas_line_type"
                      :key="index"
                      v-if="item.seq>0"
                      :label="item.label"
                      :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="客户区域">
                <treeselect
                    v-model="form.customerAreaId"
                    :options="areaList"
                    placeholder="选择客户区域"
                    style="width: 370px"
                    :normalizer="normalizer"
                >
                </treeselect>
              </el-form-item>
              <el-form-item label="客户数">
                <el-input v-model="form.customerNum" readonly style="width: 370px;"/>
              </el-form-item>
              <el-form-item label="线路路程">
                <el-input v-model="form.lineDistance" style="width: 370px;"/>
              </el-form-item>
              <el-form-item label="线路补助">
                <el-input v-model="form.lineSubsidy" style="width: 370px;"/>
              </el-form-item>
              <el-form-item label="线路津贴">
                <el-input v-model="form.lineAllowance" style="width: 370px;"/>
              </el-form-item>
<!--              <el-form-item label="排序">-->
<!--                <el-input-number v-model="form.seq" :min="1" label="排序" style="width: 370px;"></el-input-number>-->
<!--              </el-form-item>-->
              <el-form-item label="状态" prop="status">
                <el-select v-model="form.status" class="edit-input" placeholder="请选择" style="width: 370px;">
                  <el-option
                      v-for="(item,index) in dict.common_status"
                      :key="index"
                      v-if="item.seq>0"
                      :label="item.label"
                      :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="form.remark" type="textarea" style="width: 370px;"/>
              </el-form-item>
            </el-tab-pane>
            <el-tab-pane label="线路车辆" name="car">
              <zSelect @change="o=>tmp.car=o" url="api/dropDown/basCarList" placeholder="选择车辆进行添加"
                       style="width:  370px"/>

              <div style="margin-top:10px">
                <el-button
                    class="filter-item"
                    size="mini"
                    type="primary"
                    :disabled="tmp.car.id==null"
                    @click="addCar"
                >
                  新增
                </el-button>
                <el-button
                    class="filter-item"
                    size="mini"
                    type="danger"
                    @click="toDeleteCarList"
                >
                  删除
                </el-button>
              </div>
              <el-table ref="carListTable" stripe border :data="form.lineCarList" height="200" size="small"
                        style="width: 100%;margin-top:20px">
                <el-table-column :render-header="crud.renderHeader" type="selection" width="60"/>
                <el-table-column :render-header="crud.renderHeader" type="index" width="60" label="序号"/>
                <el-table-column :render-header="crud.renderHeader" prop="name" label="名称">
                  <template slot-scope="scope">
                    {{ scope.row.carName || scope.row.name }}
                  </template>
                </el-table-column>
                <el-table-column :render-header="crud.renderHeader" prop="code" label="编码">
                  <template slot-scope="scope">
                    {{ scope.row.carCode || scope.row.code }}
                  </template>
                </el-table-column>
                <!--                <el-table-column :render-header="crud.renderHeader" prop="type" label="车辆类型"/>-->
                <!--                <el-table-column :render-header="crud.renderHeader" prop="carLicense" label="车牌号"/>-->
                <!--                <el-table-column :render-header="crud.renderHeader" prop="maxBoxCount" label="装载箱数"/>-->
                <!--                <el-table-column :render-header="crud.renderHeader" prop="maxVolume" label="装载体积"/>-->
                <!--                <el-table-column :render-header="crud.renderHeader" prop="maxWeight" label="装载重量"/>-->
                <!--                <el-table-column :render-header="crud.renderHeader" prop="driverUserName" label="默认司机"/>-->
                <!--                <el-table-column :render-header="crud.renderHeader" prop="followUserName" label="默认跟车"/>-->
              </el-table>
            </el-tab-pane>
            <el-tab-pane label="线路客户" name="customer">
              <zSelect @change="o=>tmp.customer=o" url="api/dropDown/basCustomerList" placeholder="选择客户进行添加"
                       style="width: 370px"/>
              <div style="margin-top:10px">
              <el-button
                  class="filter-item"
                  size="mini"
                  type="primary"
                  :disabled="tmp.customer.id==null"
                  @click="addCustomer"
              >
                新增
              </el-button>
              <el-button
                  class="filter-item"
                  size="mini"
                  type="danger"
                  @click="toDeleteCustomerList"
              >
                删除
              </el-button>
              </div>

              <el-table ref="customerListTable" stripe :data="form.lineCustomerList" height="200" size="small"
                        style="width: 100%;margin-top:20px">
                <el-table-column :render-header="crud.renderHeader" type="selection" width="60"/>
                <el-table-column :render-header="crud.renderHeader" type="index" width="60" label="序号"/>
                <el-table-column :render-header="crud.renderHeader" prop="name" label="名称">
                  <template slot-scope="scope">
                    {{ scope.row.customerName || scope.row.name }}
                  </template>
                </el-table-column>
                <el-table-column :render-header="crud.renderHeader" prop="code" label="编码">
                  <template slot-scope="scope">
                    {{ scope.row.customerCode || scope.row.code }}
                  </template>
                </el-table-column>
                <el-table-column :render-header="crud.renderHeader" prop="fromBasCustomer" label="来自客户档案">
                  <template slot-scope="scope">
                    <span v-show="scope.row.fromBasCustomer===0">否</span>
                    <span v-show="scope.row.fromBasCustomer===1">是</span>
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
          </el-tabs>


        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>

      <vue-context-menu :contextMenuData="contextMenuData"
                        @tableConfig="tableConfig"
                        @deletedata="deletedata"></vue-context-menu>
      <!-- 表格配置 -->
      <el-dialog title="表格配置"
                 :visible.sync="tableConfigShow"
                 width="60%">
        <j-table-column-config :columns.sync="crud.header"  @tabSave="tabSave" @pit="pit" />
      </el-dialog>

      <!--表格渲染-->

      <el-table ref="table"  border
                height="700"
                show-summary
                @cell-dblclick="row=>crud.toEdit(row)"
                v-loading="crud.loading"
                :data="crud.data"
                size="small"
                style="width: 100%;"
                v-if="isShowTable"
                @header-dragend="changeColWidth"
                @contextmenu.native="showMenu"
                :summary-method="crud.sumRow"
                @row-click="crud.selectRow"
                :row-style="crud.rowClass"
                :row-class-name="crud.tableRowClassName"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column :render-header="crud.renderHeader" type="selection" width="60"/>
        <el-table-column :render-header="crud.renderHeader" type="index" width="60" label="序号"/>
        <el-table-column
          v-if="item.visible"
          v-for="(item, index) in crud.header"
          :key="`col_${index}`"
          :prop="item.prop"
          :label="columnLabel(item)"
          :width="item.width + 'px'"
          :data-porp='item.prop'
          @contextmenu.native="showMenu"
          @header-dragend="changeColWidth"
        >
          <!-- 自定义渲染模版 -->
          <template slot-scope="scope">
            <!-- 当列key为 sort 时，渲染一个手柄icon -->
            <template v-if="item.prop =='sort'">
              <div class="handle_move">
                <i :class="scope.row['sort']" />
              </div>
            </template>
            <template v-else>
              <span v-if="item.type == 1" style="margin-left: 0px">{{scope.row[`${item.prop}`]}}</span>
              <span v-if="item.type == 2" style="margin-left: 0px">{{ parseTime(scope.row[`${item.prop}`])}}</span>
              <span v-if="item.type == 3" style="margin-left: 0px">{{ getDict(`this.dict.label.${item.dictionary}["${scope.row[item.prop]}"]`) }}</span>
            </template>
          </template>
        </el-table-column>
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('code')" prop="code" label="编码"/>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('name')" prop="name" label="名称"/>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('customerAreaName')" prop="customerAreaName" label="客户区域名称"/>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('customerNum')" prop="customerNum" label="客户数"/>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('type')" prop="type" label="线路复杂度">-->
<!--          <template slot-scope="scope">-->
<!--            {{ dict.label.t_bas_line_type[scope.row.type] }}-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        &lt;!&ndash;        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('lineDistance')" prop="lineDistance" label="线路路程"/>&ndash;&gt;-->
<!--        &lt;!&ndash;        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('lineSubsidy')" prop="lineSubsidy" label="线路补助"/>&ndash;&gt;-->
<!--        &lt;!&ndash;        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('lineAllowance')" prop="lineAllowance" label="线路津贴"/>&ndash;&gt;-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('seq')" prop="seq" label="排序"/>-->
<!--           <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('status')" prop="status" label="状态">-->
<!--          <template slot-scope="scope">-->
<!--            {{ dict.label.common_status[scope.row.status] }}-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('remark')" prop="remark" label="备注"/>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-permission="['admin','basLine:edit','basLine:del']" label="操作" width="150px" align="center">-->
<!--          <template slot-scope="scope">-->
<!--            <udOperation-->
<!--                :data="scope.row"-->
<!--                :permission="permission"-->
<!--            />-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudBasLine from '@/api/erp/basLine'
import CRUD, {crud, form, header, presenter} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {getAreaTree} from "@/api/erp/basCustomerArea";
import Treeselect from "@riophae/vue-treeselect";

import { publicListKey } from "@/utils/key";
import {getHeaderProperties} from "@/api/erp/headerProperties";
import {tablemixins} from "@/mixins/tablemixins";
import  jTableColumnConfig  from "@/components/jTableColumnConfig/jTableColumnConfig";

// crud交由presenter持有
const defaultCrud = CRUD({title: '线路', url: 'api/basLine', sort: 'code,asc', crudMethod: {...crudBasLine}})
const defaultForm = {
  id: null,
  code: null,
  name: null,
  type: null,
  customerAreaId: null,
  customerAreaCode: null,
  customerAreaName: null,
  customerNum: null,
  lineCarList: [],
  lineCustomerList: [],
  lineDistance: null,
  lineSubsidy: null,
  lineAllowance: null,
  seq: null,
  status: null,
  remark: null,
  createUserId: null,
  createUserCode: null,
  createUserName: null,
  createTime: null,
  updateUserId: null,
  updateUserCode: null,
  updateUserName: null,
  updateTime: null
}
export default {
  name: 'BasLine',
  components: {pagination, crudOperation, rrOperation, udOperation, Treeselect,jTableColumnConfig},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud(),publicListKey,tablemixins],
  dicts: ['t_bas_line_type', 'common_status'],
  data() {
    return {

      permission: {
        add: ['admin', 'basLine:add'],
        edit: ['admin', 'basLine:edit'],
        del: ['admin', 'basLine:del'],
        import: ['admin', 'basLine:import'],
        export: ['admin', 'basLine:export'],
        copy: ['admin', 'basLine:copy'],
        upload: ['admin', 'basLine:upload'],
        download: ['admin', 'basLine:download'],
        enable: ['admin', 'basLine:enable'],
        disable: ['admin', 'basLine:disable'],
      },
      rules: {
        name: [
          {required: true, message: '名称不能为空', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '编码不能为空', trigger: 'blur'}
        ],
      },
      areaList: [],
      activeName: 'info',
      tmp: {
        car: {},
        customer: {}
      }
    }
  },
  watch: {
    tmp:{
      handler:function(n){
        console.log(n);
      },
      deep:true
    },
    "form.lineCustomerList"(){
      console.log(this.form.lineCustomerList)
      this.form.customerNum = this.form.lineCustomerList.length
    }
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      getAreaTree().then(res => {
        this.areaList = res
      })
    },
    normalizer(node) {
      //去掉children=[]的children属性
      if (node.children == null || node.children.length == 0) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    toDeleteCarList() {
      let _arr = []
      let arr = []
      for (let b of this.$refs.carListTable.selection) {
        arr.push(b.id)
      }

      this.form.lineCarList.forEach(o => {
        if (arr.indexOf(o.id) < 0) {
          _arr.push(o)
        }
      })
      this.form.lineCarList = []
      this.form.lineCarList = _arr
    },
    toDeleteCustomerList() {

      let _arr = []
      let arr = []
      for (let b of this.$refs.customerListTable.selection) {
        arr.push(b.id)
      }
      console.log(arr);
      this.form.lineCustomerList.forEach(o => {
        if (arr.indexOf(o.id) < 0) {
          _arr.push(o)
        }
      })
      this.form.lineCustomerList = []
      this.form.lineCustomerList = _arr
    },
    addCar() {
      if (this.form.lineCarList == null) {
        this.form.lineCarList = []
      }
      //判断下是否已经存在
      let obj = this.form.lineCarList.find(o => {
        return o.id == this.tmp.car.id
      })
      if (obj) {
        this.$message.error("车辆-" + this.tmp.car.name + "已经添加")
        return false;
      }
      this.tmp.car.carId = this.tmp.car.id
      this.tmp.car.id = null
      this.form.lineCarList.push(this.tmp.car)
    },
    addCustomer() {
      if (this.form.lineCustomerList == null) {
        this.form.lineCustomerList = []
      }
      console.log(this.tmp.customer);
      //判断下是否已经存在
      let obj = this.form.lineCustomerList.find(o => {
        return o.id == this.tmp.customer.id
      })
      console.log(obj);
      if (obj) {
        this.$message.error("客户-" + this.tmp.customer.name + "已经添加")
        return false;
      }
      this.tmp.customer.customerId = this.tmp.customer.id
      this.form.lineCustomerList.push(this.tmp.customer)
    }


  }
}
</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
</style>
